package cn.tude._05mvcboot04.controller;


import cn.tude._05mvcboot04.base.response.JsonResult;
import cn.tude._05mvcboot04.base.response.StatusCode;
import cn.tude._05mvcboot04.mapper.ArticleMapper;
import cn.tude._05mvcboot04.pojo.dto.ArticleDTO;
import cn.tude._05mvcboot04.pojo.entity.Article;
import cn.tude._05mvcboot04.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("v1/article/")
@Api(tags = "02.文章模块")
@Validated
public class ArticleController {
    @Autowired(required = false)
    private ArticleMapper articleMapper;


    @PostMapping("insert")
    @ApiOperation(value = "1.发布文章")
    public JsonResult insertArticle(@RequestBody @Validated ArticleDTO articleDTO , @ApiIgnore HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        if (userVO!=null){
            Article article=new Article();
            BeanUtils.copyProperties(articleDTO,article);
            article.setUserId(userVO.getId());
            int i = articleMapper.insertAritcle(article);
            if (i==1)
                return JsonResult.ok();
            return JsonResult.notok();
        }
        return new JsonResult(StatusCode.NOT_LOGIN);
    }


    @GetMapping("selectById")
    @ApiOperation(value = "2.文章详细信息")
    public JsonResult selectArticleById(@Range(min = 1,max = 200,message = "文章id应该在1~200之间") @RequestParam Long id){
        return JsonResult.ok(articleMapper.selectArticleById(id));
    }



    @GetMapping("allarticle")
    @ApiOperation(value = "3.获取所有文章")
    public JsonResult selectAllArticle(){
        return JsonResult.ok(articleMapper.selectAllArticle());
    }
}
